package com.somnus.demo.androiddatabingdemo.adapter;

import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;


import com.somnus.demo.androiddatabingdemo.BR;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.somnus.demo.androiddatabingdemo.R;
import com.somnus.demo.androiddatabingdemo.bean.UserBean;
import com.somnus.demo.androiddatabingdemo.databinding.ItemRecyclerBinding;
import com.somnus.demo.androiddatabingdemo.listener.MyPresenter;

/**
 * Created by Somnus on 2017/3/13.
 *
 * @Description:
 */

public class RecyclerViewBaseAdapter extends BaseQuickAdapter<UserBean, RecyclerViewBaseAdapter.MyViewHolder> {
    private MyPresenter mPresenter;

    public RecyclerViewBaseAdapter() {
        super(R.layout.item_recycler);
        mPresenter = new MyPresenter();
    }

    @Override
    protected void convert(MyViewHolder helper, UserBean item) {

        ItemRecyclerBinding itemRecyclerBinding = helper.getBinding();
        itemRecyclerBinding.setVariable(BR.user,item);
        itemRecyclerBinding.setVariable(BR.presenter,mPresenter);
        itemRecyclerBinding.executePendingBindings();

    }

    @Override
    protected MyViewHolder createBaseViewHolder(View view) {
        return new MyViewHolder(view);
    }

    @Override
    protected View getItemView(int layoutResId, ViewGroup parent) {

        ItemRecyclerBinding binding = DataBindingUtil.inflate(mLayoutInflater, layoutResId, parent, false);
        if (binding == null) {
            return super.getItemView(layoutResId, parent);
        }
        View view = binding.getRoot();
        view.setTag(R.id.MyAdapter_databing_support,binding);

        return view;
    }

    public class MyViewHolder extends BaseViewHolder {
        public MyViewHolder(View view) {
            super(view);
        }

        public ItemRecyclerBinding getBinding() {
            return (ItemRecyclerBinding) getConvertView().getTag(R.id.MyAdapter_databing_support);
        }
    }
}
